/*
 * @Author: Haochen
 * @Date: 2021-12-10 17:41:01
 * @LastEditTime: 2021-12-11 11:29:47
 * @FilePath: \students-system\src\store\modules\students.js
 */

import api from '@/api/api.js';

export default {
  namespaced: true,
  state: {
    rows: [],
    pages: 0,
    total: 0,
    editData: {},
  },
  mutations: {
    SET_STUDENTS(state, payload) {
      Object.assign(state, payload);
    },

    SET_STUDENTS_BY_ID(state, payload) {
      Object.assign(state.editData, payload);
    },
  },
  actions: {
    async getStudentsAsync(context, payload) {
      const { data: res } = await api.students.getStudents(payload);

      if (res.code) {
        context.commit('SET_STUDENTS', res.data);
        return res.data;
      }
    },

    async getStudentsByIdAsync(context, payload) {
      const { data: res } = await api.students.getStudentById(payload);
      if (res.code) {
        context.commit('SET_STUDENTS_BY_ID', res.data);
        return res;
      }
    },
  },
};
